package com.xyu.gulimall.product.web;

import com.xyu.gulimall.product.entity.CategoryEntity;
import com.xyu.gulimall.product.service.CategoryService;
import com.xyu.gulimall.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询一级分类
     * @param model
     * @return
     */
    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        // TODO 查询所用一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevelCategorys();
        //返回数据
        model.addAttribute("categorys",categoryEntities);
        return "index";
    }
    // index/catalog.json
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String,List<Catelog2Vo>> getCatalogJson(){
        //返回二级三级分类的JSON
//        Map<String,List<Catelog2Vo>> categoryServiceCatelogJson = categoryService.getCatalogJsonRedis();
        Map<String,List<Catelog2Vo>> categoryServiceCatelogJson = categoryService.getCatelogSeataJson();
//        Iterator<Map.Entry<String, List<Catelog2Vo>>> iterator = categoryServiceCatelogJson.entrySet().iterator();
//        while (iterator.hasNext()){
//            Map.Entry<String, List<Catelog2Vo>> key = iterator.next();
//            System.out.println("key"+key.getKey()+"\n"+key.getValue());
//        }
        return categoryServiceCatelogJson;
    }
}
